<?php

namespace App\Http\Controllers\Web;

use App\Category;
use App\Post;
use App\Setting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    //
    private $postManager;
    private $settingManager;
    private $categoryManager;
    private $request;
    public function __construct (Request $request)
    {
        $this->postManager = new Post();
        $this->settingManager = new Setting();
        $this->categoryManager = new Category();
        $this->request = $request;
    }

    /**
     *文章列表
     */
    public function index($slug){
        try{
            $category = $this->categoryManager->where('slug',$slug)->first()->toArray();
        }catch (\Exception $e){
            throw new \Exception('该栏目不存在');
        }
        //获得栏目模板
        if(!$category['tpl']&&$category['pid']){
            $parent = $this->categoryManager->select('tpl')->where('id',$category['pid'])->first()->toArray();
            if($parent['tpl'])$category['tpl'] = $parent['tpl'];
        }
        $template = $category['tpl']?:'index';
        return view('web.posts.list.'.$template,
            [
                'slug'=>$slug,
                'request'=>$this->request,
                'category'=>$category]
        );
    }
    public function ajaxList(){
        $id = $this->request->category_id;
        try{
            $category = $this->categoryManager->where('id',$id)->first()->toArray();
        }catch (\Exception $e){
            throw new \Exception('该栏目不存在');
        }
        //获得栏目模板
        if(!$category['tpl']&&$category['pid']){
            $parent = $this->categoryManager->select('tpl')->where('id',$category['pid'])->first()->toArray();
            if($parent['tpl'])$category['tpl'] = $parent['tpl'];
        }
        $template = $category['tpl']?:'index';
        $this->request->category_id = $category['id'];
        $this->request->status = Post::PUBLISHED;
        $list =  $this->postManager->getPageList($this->request,true);
        if($list){
            return view('web.posts.list-components.'.$template,[
                'list' =>  $list
            ]);
        }
        else{
            return '';
        }
    }

    public function detail($id){
        $post = $this->postManager->getPost($id);

        if(!$post){
            abort(404);
        }
        if($post->use_link==Post::USE_LINK){
            return redirect($post->link);
        }
        $template = $post['tpl']?:'';
        if(!$template) {
            //查询栏目主题
            $category = $this->categoryManager->where('id', $post['category_id'])->first()->toArray();
            if (!$category['tpl']) {
                $parent = $this->categoryManager->select('tpl')->where('id', $category['pid'])->first()->toArray();
                if ($parent['tpl']) $category['tpl'] = $parent['tpl'];
            }
            if($category['tpl']){
                $template = $category['tpl'];

            }else{
                $template = 'index';
            }
        }
        return view('web.posts.view.'.$template,[
            'post'  =>  $post,
//            'next'  =>  $this->postManager->getNext($id,$post->category_id),
//            'prev'  =>  $this->postManager->getPrev($id,$post->category_id)
        ]);
    }

    public function channel($slug){
    }
}
